package com.ruoyi.web.controller.qiushi.mall;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.qiushi.domain.mall.ProductSpuFile;
import com.ruoyi.qiushi.service.mall.IProductSpuFileService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品图片视频Controller
 *
 * @author ruoyi
 * @date 2023-12-27
 */
@RestController
@RequestMapping("/qiushi/mall/productspufile")
public class ProductSpuFileController extends BaseController {
    @Autowired
    private IProductSpuFileService productSpuFileService;

    /**
     * 查询商品图片视频列表
     */
    @PreAuthorize("@ss.hasPermi('qiushi:mall:productspufile:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductSpuFile productSpuFile) {
        startPage();
        List<ProductSpuFile> list = productSpuFileService.selectProductSpuFileList(productSpuFile);
        return getDataTable(list);
    }

    /**
     * 导出商品图片视频列表
     */
    @PreAuthorize("@ss.hasPermi('qiushi:mall:productspufile:export')")
    @Log(title = "商品图片视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductSpuFile productSpuFile) {
        List<ProductSpuFile> list = productSpuFileService.selectProductSpuFileList(productSpuFile);
        ExcelUtil<ProductSpuFile> util = new ExcelUtil<ProductSpuFile>(ProductSpuFile.class);
        util.exportExcel(response, list, "商品图片视频数据");
    }

    /**
     * 获取商品图片视频详细信息
     */
    @PreAuthorize("@ss.hasPermi('qiushi:mall:productspufile:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(productSpuFileService.selectProductSpuFileById(id));
    }

    /**
     * 新增商品图片视频
     */
    @PreAuthorize("@ss.hasPermi('qiushi:mall:productspufile:add')")
    @Log(title = "商品图片视频", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductSpuFile productSpuFile) {
        productSpuFile.setUpdateTime(new Date());
        return toAjax(productSpuFileService.insertProductSpuFile(productSpuFile));
    }

    /**
     * 修改商品图片视频
     */
    @PreAuthorize("@ss.hasPermi('qiushi:mall:productspufile:edit')")
    @Log(title = "商品图片视频", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductSpuFile productSpuFile) {
        productSpuFileService.updateProductSpuFile(productSpuFile);

//        ProductSpuFile temp = new ProductSpuFile();
//        temp.setSpuId(productSpuFile.getSpuId());
//        List<ProductSpuFile> list = productSpuFileService.selectProductSpuFileList(temp);
//        for (int i = 0; i < list.size(); i++) {
//
//            list.get(i).setSort((long) i + 1);
//            list.get(i).setUpdateTime(new Date());
//
//            productSpuFileService.updateProductSpuFile(list.get(i));
//        }

        return AjaxResult.success();
    }

    /**
     * 删除商品图片视频
     */
    @PreAuthorize("@ss.hasPermi('qiushi:mall:productspufile:remove')")
    @Log(title = "商品图片视频", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        ProductSpuFile productSpuFile = new ProductSpuFile();
        productSpuFile.setUpdateTime(new Date());
        productSpuFile.setDelFlag("1");
        productSpuFile.setId(id);
        return toAjax(productSpuFileService.updateProductSpuFile(productSpuFile));
    }
}
